package com.ea.eadp.nexus.test.pageDefinition.pc;

import com.ea.eadp.nexus.test.common.TestUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import com.ea.eadp.nexus.test.common.SystemProperties;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;

public class ParentEmailChallengePage extends BasePage {

    public ParentEmailChallengePage(WebDriver driver) {
        super(driver, ParentEmailChallengePage.class);

    }
    
    protected static final String redirectURI =
        SystemProperties.instance().getProperty(SystemProperties.RedirectUri);
    
    public WebElement txt_parentEmailAddress;
    public WebElement btn_updateParentEmail;
    public WebElement txt_msgParentEmailRequired;
    public WebElement btn_resend;
    public WebElement btn_next;
    public WebElement btn_cancel;
    public WebElement txt_errorMessage;
    
    public static class ControlDefinitions {
        public static By txt_parentEmailAddress = By.id("parent-email-address");
        public static By btn_updateParentEmail = By.id("update-parent-email-link");
        public static By txt_msgParentEmailRequired = By.id("msg-parent-email-required");
        public static By btn_resend = By.id("resendBtn");
        public static By btn_next = By.xpath("//div[@id='right-column-action-area']/a[1]");
        public static By btn_cancel = By.xpath("//div[@id='right-column-action-area']/a[2]");
        public static By txt_errorMessage = By.xpath("//div[@id='online-general-error']/div");
    }
    
    public void initControls() {
        this.txt_parentEmailAddress = driver.findElement(ControlDefinitions.txt_parentEmailAddress);
        this.btn_updateParentEmail = driver.findElement(ControlDefinitions.btn_updateParentEmail);
        this.txt_msgParentEmailRequired = driver.findElement(ControlDefinitions.txt_msgParentEmailRequired);
        this.btn_resend = driver.findElement(ControlDefinitions.btn_resend);
        this.btn_next = driver.findElement(ControlDefinitions.btn_next);
        this.btn_cancel = driver.findElement(ControlDefinitions.btn_cancel);
    }
    
    public void initPage() {
        waitForWebElementPresent(ControlDefinitions.btn_next);
        initControls();
    }
    
    public void assertLoginComplete() throws Exception {
        Thread.sleep(5000);
        String url = TestUtil.getCurrentUrl(driver);
        Assert.assertTrue(
            url.contains(redirectURI) && url.contains("code="),
            "Verify login complete failed."
        );
    }
    
    public void assertVerifyFailed() throws Exception {
        this.txt_errorMessage =
            driver.findElement(ControlDefinitions.txt_errorMessage);
        Assert.assertTrue(
            this.txt_errorMessage.getText().contains(
                "We have not received your email verification." +
                " Please complete this step before you continue"
            )
        );
    }
    
}
